package com.my.test.java.zhtx;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.my.test.java.two.jdbc.DBConnection;

public class DemoDate {
	static Connection con = DBConnection.getConn("jdbc:mysql://192.168.102.101:32306/dxh_base","hzb","zhtx2016");
	
	public static List<Map> getAll(int start,int pageSize){
		String sql = "select * from storesku where df=0 limit ?,?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Map> list = new ArrayList<Map>();
		try{
			ps = con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("vendorId", rs.getLong("vendorId"));
				map.put("storeCode", rs.getString("storeCode"));
				map.put("skuCode", rs.getString("skuCode"));
				list.add(map);
			}
			System.out.println("sql="+sql+",start="+start+",pageSize="+pageSize+",list="+list.size());
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	public static void getStoreSku(Map map){
		String sql = "select * from storesku where df=0 and vendorId=? and storeCode=? and skuCode=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Map> list = new ArrayList<Map>();
		try{
			ps = con.prepareStatement(sql);
			ps.setLong(1, Long.parseLong(map.get("vendorId").toString()));
			ps.setString(2, map.get("storeCode").toString());
			ps.setString(3, map.get("skuCode").toString());
			rs = ps.executeQuery();
			while(rs.next()){
				System.out.println(Thread.currentThread().getName()+",skuName="+rs.getString("skuName"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void test(int count){
//		List<Map> list = getAll(0,10);
//		for(Map m:list){
//			getStoreSku(m);
//		}
		for(int j=0;j<count;j++){
			new Thread(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+"start.........");
                    String name = Thread.currentThread().getName().replace("Thread-", "");
                    System.out.println(name);
                    int pageSize =100;
                    int count = 10;
                    int start = pageSize*count*Integer.parseInt(name);
                    for(int i=0;i<100;i++) {
//                        Map map = new HashMap();
//                        map.put("start", start+pageSize*i);
//                        map.put("pageSize", pageSize);
                        List<Map> list = getAll(start+pageSize*i,pageSize);
//                        System.out.println("list="+list.size());
                        for (Map m : list) {
                        	getStoreSku(m);
                        }
                    }
				}
			}).start();
		}
	}
	
	public static void main(String[] args) {
		test(20);
	}
	
}
